#include<iostream>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;

class Solution {
public:
    int minCost(vector<vector<int>>& costs) {
        int n = costs.size();
        vector<int> r(n + 1);
        vector<int> b(n + 1);
        vector<int> g(n + 1);
        for (int i = 0; i < n; ++i)
        {
            r[i + 1] = min(b[i], g[i]) + costs[i][0];
            g[i + 1] = min(r[i], b[i]) + costs[i][2];
            b[i + 1] = min(r[i], g[i]) + costs[i][1];
        }
        return min(r[n], min(b[n], g[n]));
    }
};